Spring Boot 您所在的位置:网站首页 spring boot 监控组件 Spring Boot

Spring Boot

2023-08-06 05:03| 来源: 网络整理| 查看: 265

文章目录 Spring Boot —— Actuator 监控、检测、审计、应用情况采集前言组成部分Endpoints内置Endpoints 配置配置pom.xml如果要访问info接口想获取maven中的属性内容请记得添加如下内容 yml配置actuator自定义Actuator(学习重点)示例1:健康端点示例2:健康端点自定义端点实现自定义端点注解介绍自定义端点示例编写自定义端点必不可少的@Bean准备就绪,测试结果 项目地址总结

Spring Boot —— Actuator 监控、检测、审计、应用情况采集 前言

是Spring Boot 一个非常强大功能,可以对应用程序进行监视和管理,通过restful api请求来监管、审计、收集应用的运行情况,针对微服务而言它是必不可少的一个组件。

组成部分 Endpoints

actuator核心部分,用来监视应用程序及交互,spring-boot-actuator中已经内置非常多的Endpoints(health、info、beans、httptrace、shutdown等等),同时,也允许程序员自定义扩展端点.

Spring Boot 2.0 中的端点相较之前版本有较大不同,使用时需注意。另外端点的监控机制也有很大的不同,启用了不代表可以直接访问,还需要将其暴露出来,传统的management.security管理已被标记为不推荐。

内置Endpoints iddescSensitiveauditevents显示当前应用程序的审计事件信息Yesbeans显示应用Spring Beans的完整列表Yescaches显示可用缓存信息Yesconditions显示自动装配类的状态及及应用信息Yesconfigprops显示所有 @ConfigurationProperties 列表Yesenv显示 ConfigurableEnvironment 中的属性Yesflyway显示 Flyway 数据库迁移信息Yeshealth显示应用的健康信息(未认证只显示status,认证显示全部信息详情)Noinfo显示任意的应用信息(在资源文件写info.xxx即可)Noliquibase展示Liquibase 数据库迁移Yesmetrics展示当前应用的 metrics 信息Yesmappings显示所有 @RequestMapping 路径集列表Yesscheduledtasks显示应用程序中的计划任务Yessessions允许从Spring会话支持的会话存储中检索和删除用户会话Yesshutdown允许应用以优雅的方式关闭(默认情况下不启用)Yesthreaddump执行一个线程dumpYeshttptrace显示HTTP跟踪信息(默认显示最后100个HTTP请求 - 响应交换)Yes 配置 配置pom.xml org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-starter-web 如果要访问info接口想获取maven中的属性内容请记得添加如下内容 org.springframework.boot spring-boot-maven-plugin build-info yml配置actuator

yml中配置actuator,其中info开头的属性,就是访问info端点中显示的相关内容,值得注意的是Spring Boot2.x中,默认只开放了info、health两个端点,其余需要自己通过配置management.endpoints.web.exposure.include属性来加载(包含include 自然就有 exclue,等其它属性)。如果想单独操作某个端点,可使用management.endpoint.端点.enabled属性进行启用或禁用。

# 描述信息 info: blog-url: http://winterchen.com author: Luis # 能获取到maven中配置的版本信息 version: @project.version@ # 加载所有的端点/默认只加载了 info/health management: endpoints: web: exposure: include: "*"“ endpoint: health: show-details: always # 关闭指定的端点 shutdown: enabled: false # # 路径映射,将health路径映射成rest_health,那么在访问health路径将为404,因为原请求地址"health"已变成"rest_health",通常不建议使用此功能: # web: # path-mapping: # health: rest_health 启动项目,并访问 http://localhost:8082/actuator/info {"blog-url":"http://winterchen.com","author":"Luis","version":"1.0-SNAPSHOT"} 自定义Actuator(学习重点)

以上是默认的以及自带的配置,实际应用中有时候默认并不能满足我们的要求,比如Spring Boot默认的的配置并不能满足实际应用需求。

默认装配 HealthIndicators 下列是依赖spring-boot-xxx-starter后相关HealthIndicator的实现(通过maangement.health.defaults.enabled属性可以禁用它们),但想要获取一些特定的,例如监控某个特定业务是否可用时,就需要自定义HealthIndicator了 属性描述CassandraHealthIndicator检查Cassandra数据库是否启动DiskSpaceHealthIndicator检查磁盘空间不足DataSourceHealthIndicator检查是否可以获得连接DataSourceElasticsearchHealthIndicator检查Elasticsearch集群是否启动InfluxDbHealthIndicator检查InfluxDB服务器是否启动JmsHealthIndicator检查JMS代理是否启动MailHealthIndicator检查邮件服务器是否启动MongoHealthIndicator检查Mongo数据库是否启动Neo4jHealthIndicator检查Neo4j服务器是否启动RabbitHealthIndicator检查Rabbit服务器是否启动RedisHealthIndicator检查Redis服务器是否启动SolrHealthIndicator检查Solr服务器是否启动 示例1:健康端点

要求:实现HealthIndicator接口,自定义检测内容,并返回状态UP还是DOWN,来尝试一下吧。

package com.frank.sb.actuator.config; import org.springframework.boot.actuate.health.Health; import org.springframework.boot.actuate.health.HealthIndicator; import org.springframework.stereotype.Component; @Component("frankHealthIndicator_1") public class MyHealthIndicator implements HealthIndicator { private static final String VERSION = "V1.0.0"; /** * 自定义检测check函数内容是否为0的例子。 * 我们可以访问 * @return */ @Override public Health health() { // 获取自定义check内容 int code = check(); // 当check不等于0时,DOWN:代表运行错误 if(code != 0) { Health.down().withDetail("code", code).withDetail("version", VERSION).build(); } // 当check结果为0时,UP:代表运行正常 return Health.up().withDetail("code", code).withDetail("version", VERSION).up().build(); } private int check() { return 0; } } 访问查看结果

http://localhost:8082/actuator/health

{ "status": "UP", "details": { "frank": { "status": "UP", "details": { "code": 0, "version": "V1.0.0" } }, "diskSpace": { "status": "UP", "details": { "total": 500068036608, "free": 50143068160, "threshold": 10485760 } } } } 示例2:健康端点

继承AbstractHealthIndicator抽象类,重写doHealthCheck方法,功能比示例1更强大,默认DataSourceHealthIndicator、RedisHealthIndicator都是这种写法,内容回调中还做了异常处理。 代码示例如下:

package com.frank.sb.actuator.config; import org.springframework.boot.actuate.health.AbstractHealthIndicator; import org.springframework.boot.actuate.health.Health; import org.springframework.stereotype.Component; /** * 自定义健康端点 * 功能强大一些。 * DataSourceHealthIndicator 与 RedisHealthIndicator 写法相同 */ @Component("frankHealthIndicator_2") public class MyAbstractHealthIndicator extends AbstractHealthIndicator { private static final String VERSION = "V1.0.0"; @Override protected void doHealthCheck(Health.Builder builder) throws Exception { int code = check(); if(code != 0) { builder.down().withDetail("code", code).withDetail("version", VERSION).build(); } builder.withDetail("code", code).withDetail("version", VERSION).up().build(); } private int check() { return 0; } } 访问查看结果

http://localhost:8082/actuator/health

{ "status": "UP", "details": { "frank": { "status": "UP", "details": { "code": 0, "version": "V1.0.0" } }, "diskSpace": { "status": "UP", "details": { "total": 500068036608, "free": 50143068160, "threshold": 10485760 } } } }

如果能访问到,并显示上面信息,代表配置成功。

自定义端点

info、health都是spring-boot-actuator内置的,实现自定义端点需要使用@Endpoint、@ReadOperation、@WriteOperation、@DeleteOperation 注解。

实现自定义端点注解介绍 注解解释@Endpoint构建 rest api 的唯一路径@ReadOperationGET请求,响应状态为 200 如果没有返回值响应 404(资源未找到)@WriteOperationPOST请求,响应状态为 200 如果没有返回值响应 204(无响应内容)@DeleteOperationDELETE请求,响应状态为 200 如果没有返回值响应 204(无响应内容) 自定义端点示例 编写自定义端点 package com.frank.sb.actuator.config; import org.springframework.boot.actuate.endpoint.annotation.Endpoint; import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; import java.util.HashMap; import java.util.Map; /** *

@Endpoint 是构建 rest 的唯一方式

* 不同请求的操作,调用时缺少必需参数,或者使用无法转换为所需类型的参数,则不会调用操作方法,响应状态将为400(错误请求) *

@ReadOperation = GET 响应状态为 200 如果没有返回值响应 404(资源未找到)

*

@WriteOperation = POST 响应状态为 200 如果没有返回值响应 204(无响应内容)

*

@DeleteOperation = DELETE 响应状态为 200 如果没有返回值响应 204(无响应内容)

* * Created by Donghua.Chen on 2018/7/12. */ @Endpoint(id = "nancy") public class MyEndPoint { @ReadOperation public Map hello() { Map result = new HashMap(); result.put("author", "Nancy"); result.put("age", "10086"); result.put("email", "[email protected]"); return result; } } 必不可少的@Bean

如果想另自定义端点生效的话,我们需要在启动类中设置一个必不可少的Bean,代码如下:

修改启动类 package com.frank.sb.actuator; import com.frank.sb.actuator.config.MyEndPoint; import org.springframework.boot.SpringApplication; import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * 启动类 */ @SpringBootApplication public class ActuatorApplication { public static void main(String[] args) { SpringApplication.run(ActuatorApplication.class, args); } /** * 启动类中,添加下面函数,将自定义端点交由spring进行管理,令其生效 */ @Configuration static class MyEndpointConfiguration { @Bean @ConditionalOnMissingBean @ConditionalOnEnabledEndpoint public MyEndPoint myEndPoint() { return new MyEndPoint(); } } } 准备就绪,测试结果

启动项目,并访问:http://localhost:8082/actuator/nancy 我们可以看到下列结果:

{ "author": "Nancy", "age": "10086", "email": "[email protected]" } 项目地址

GitHub

总结

实际投产时,我们可以根据不同的业务,自定义端点,来检测系统关键功能和组件是否可用。

本博文内容均来源于网络



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有